package com.besttone.dao;

import com.besttone.vo.HangupSmsResultVO;
import org.apache.log4j.Logger;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengzhenzhen on 2017/5/2.
 */
public class ImpalaJdbcClient {
    private static String JDBC_DRIVER = "com.cloudera.impala.jdbc41.Driver";
    //默认数据库default,默认用户名就是登录账号 密码 为空,impala server dn3.hadoop
    private static String CONNECTION_URL = "jdbc:impala://172.16.0.139:21050/default;auth=noSasl";//nn1.hadoop,database:default
    private static final Logger log = Logger.getLogger(ImpalaJdbcClient.class);
    public static void main(String[] args) {
        Connection con = null;
        ResultSet rs = null;
        PreparedStatement ps = null;

        try
        {
            Class.forName(JDBC_DRIVER);
            con = DriverManager.getConnection(CONNECTION_URL);
            ps = con.prepareStatement("select * from ti_huawei_log where day_id='20170410' limit 2;");
            rs = ps.executeQuery();
            while (rs.next())
            {
                System.out.println(rs.getString(1) + '\t' + rs.getString(2));
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        } finally
        {
            //关闭rs、ps和con
        }
    }
    /**
     *
     * @param sql
     * @return
     */
    public static List<HangupSmsResultVO> queryHangupSmsByDay(String sql){
        List<HangupSmsResultVO> voList=new ArrayList<HangupSmsResultVO>();
        try {
            Statement stmt = getDBconnection();
            System.out.println(sql);
            ResultSet res = stmt.executeQuery(sql);
            HangupSmsResultVO vo;
            while (res.next()) {
                vo=new HangupSmsResultVO();
                //System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
                vo.setSend_number(res.getString(1));
                vo.setSend_parent_account(res.getString(2));
                vo.setBusi_type(res.getString(3));
                vo.setCnts(res.getInt(4));
                vo.setDay_id(res.getString(5));
                voList.add(vo);
            }
            return voList;
        }catch (SQLException e) {
            e.printStackTrace();
            log.error("Connection error!", e);
        }
        return voList;
    }
    //get db connection
    public static Statement getDBconnection()
    {
        try {
            Class.forName(JDBC_DRIVER);
            Connection conn = DriverManager.getConnection(CONNECTION_URL);
            return conn.createStatement();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            log.error(JDBC_DRIVER + " not found!", e);
            System.exit(1);
        } catch (SQLException e) {
            e.printStackTrace();
            log.error("Connection error!", e);
            System.exit(1);
        }
        return null;
    }
}
